/***************************************************************************//**
 * @file Utils.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef UTILS_CPP
#define UTILS_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <Utils.h>

////////////////////////////////////////////////////////////////////////////////

double Utils::dayOfYear( DateTime dateTime )
{
    double doy = 0.0;

    if ( dateTime.month  <  1 ) dateTime.month  = 1;
    if ( dateTime.month  > 12 ) dateTime.month  = 12;
    if ( dateTime.day    > 31 ) dateTime.day    = 31;
    if ( dateTime.hour   > 23 ) dateTime.hour   = 23;
    if ( dateTime.minute > 59 ) dateTime.minute = 59;
    if ( dateTime.second > 59 ) dateTime.second = 59;

    switch ( dateTime.month )
    {
        case 1:  doy =   0.0; break;
        case 2:  doy =  31.0; break;
        case 3:  doy =  59.0; break;
        case 4:  doy =  90.0; break;
        case 5:  doy = 120.0; break;
        case 6:  doy = 151.0; break;
        case 7:  doy = 181.0; break;
        case 8:  doy = 212.0; break;
        case 9:  doy = 243.0; break;
        case 10: doy = 273.0; break;
        case 11: doy = 304.0; break;
        case 12: doy = 334.0; break;
    }

    doy += (double)( dateTime.day - 1.0 );
    doy += (double)dateTime.hour / 24.0;
    doy += (double)dateTime.minute / 24.0 / 60.0;
    doy += (double)dateTime.second / 24.0 / 60.0 / 60.0;

    return doy;
}

